package com.homegoods.classify.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.homegoods.classify.R;
import com.homegoods.classify.model.ClassifyModel;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.util.BindingUtil;
import java.util.List;

/**
 * add shibo.zheng
 * 19/06/22
 * 品牌fragment 中logo展示的adapter
 * （临时使用方案，可通过viewtype区分，设置不同的layout）
 */

public class ZPBrandItemAdapter extends RecyclerView.Adapter<ZPBrandItemAdapter.ZPBrandItemViewHodler> {
    private Context mContext;
    private List<ClassifyModel.BrandListBean.SubListBean> rightModels;
    private MyItemClickListener myItemClickListener;

    public ZPBrandItemAdapter(Context mContext, List<ClassifyModel.BrandListBean.SubListBean> rightModels) {
        this.mContext = mContext;
        this.rightModels = rightModels;
    }

    @NonNull
    @Override
    public ZPBrandItemViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.classify_brand_child_item, viewGroup, false);
        ZPBrandItemViewHodler viewHodler = new ZPBrandItemViewHodler(view);
        return viewHodler;
    }

    @Override
    public void onBindViewHolder(@NonNull ZPBrandItemViewHodler zpBrandItemViewHodler, int i) {
        BindingUtil.loadImageNoTransform(zpBrandItemViewHodler.brandItemImg, HomeGoodsApp.getOSSHost() + "/" + rightModels.get(i).getLogo(), null, null);
    }

    @Override
    public int getItemCount() {
        return rightModels.size();
    }

    // 自定义点击事件
    public void setMyItemClickListener(MyItemClickListener myItemClickListener) {
        this.myItemClickListener = myItemClickListener;
    }

    public interface MyItemClickListener{
        void itemClick(View view, int postion);
    }

    public class ZPBrandItemViewHodler extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView brandItemImg;

        public ZPBrandItemViewHodler(@NonNull View itemView) {
            super(itemView);
            brandItemImg = itemView.findViewById(R.id.brand_child_img);
            brandItemImg.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            myItemClickListener.itemClick(view, getPosition());
        }
    }

}
